package com.spa.domain.service;

import cn.hutool.core.bean.BeanUtil;
import com.spa.domain.entity.PlatformSysUserEntity;
import com.spa.domain.entity.SysUserEntity;
import com.spa.infrastructure.mapper.AdminUserMapperStruct;
import com.spa.infrastructure.repository.po.SysUserPO;
import com.spa.infrastructure.util.PasswdUtils;
import com.spa.infrastructure.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 平台系统用户  domain
 *
 * @author: lig
 * @date: 2023/12/7
 */
@Service
public class PlatformSysUserService {

    @Resource
    private SysAdminUserService sysUserService;

    /**
     * 添加平台的系统用户
     *
     * @author: lig
     * @date: 2023/12/6
     */
    public SysUserEntity addSysUser(PlatformSysUserEntity pSysUser) {
        SysUserEntity sysUser = sysUserService.infoOneByUsername(pSysUser.getPhone());
        if (BeanUtil.isEmpty(sysUser)) {
            String username = pSysUser.getPhone();
            SysUserPO sysUserPO = new SysUserPO();
            sysUserPO.setUserName(username);
            sysUserPO.setNickName("用户：" + username);
            sysUserPO.setPhonenumber(username);
            if(StringUtils.isNotEmpty(pSysUser.getPassword())){
                sysUserPO.setPassword(PasswdUtils.encryptPassword(pSysUser.getPassword()));
            }
            sysUserPO.setAvatar(pSysUser.getAvatar());
            sysUserService.addUser(sysUserPO);
            return AdminUserMapperStruct.INSTANCE.po2Entity(sysUserPO);
        }
        return sysUser;
    }

    public SysUserEntity infoById(Long userId) {
        return sysUserService.infoById(userId);
    }

}
